package com.kfm.web.controller.order;

import cn.hutool.json.JSON;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.kfm.common.domain.entity.Member;
import com.kfm.common.domain.entity.User;
import com.kfm.common.domain.response.Result;
import com.kfm.common.util.JwtUtils;
import com.kfm.common.util.RSAUtils;
import com.kfm.order.service.MemberService;
import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Resource
    private MemberService service;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @PostMapping("/login")
    public Result login(@RequestBody Map<String, String> map, HttpServletRequest request) throws JsonProcessingException {
        // 校验验证码
        String captcha = map.get("captcha");
        // if (!CaptchaUtil.ver(captcha, request)) {
        //     CaptchaUtil.clear(request);
        //     return Result.failure("验证码校验失败！");
        // }
        // CaptchaUtil.clear(request);
        // 获取保存私钥的 uuid
        String uuid = map.get("uuid");
        String private_key = redisTemplate.opsForValue().get(uuid);
        redisTemplate.delete(uuid);
        String password = RSAUtils.decrypt(private_key, map.get("password"));
        Member user = service.login(map.get("username"), password);
        // 登录成功 返回 token
        if (user != null) {
            String token = JwtUtils.sign(new JsonMapper().writeValueAsString(user));
            Map<String, String> result = Map.of("token", token, "username", user.getUsername());
            return Result.success("登录成功", result);

        }
        return Result.failure("登录失败");
    }

    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('member:list') or hasAnyRole('ROLE_admin')")
    public Result list() {
        List<Member> list = service.selectAll();
        return list == null ? Result.failure("查询失败") : Result.success("查询成功", list);
    }

    @PostMapping("/register")
    public Result register(@RequestBody Map<String, String> map, HttpServletRequest request) {
        String password = map.get("password");
        String repass = map.get("repass");
        if (password == null || !password.equals(repass)) {
            return Result.failure("两次密码不一致");
        }
        String username = map.get("username");
        if (username == null || username.isEmpty()) {
            return Result.failure("用户名为空");
        }
        Member user = new Member();
        user.setUsername(username);
        user.setPassword(password);
        user.setPhone(map.get("phone"));
        int insert = service.insertSelective(user);
        return insert > 0 ? Result.success("注册成功", null) : Result.failure("注册失败");
    }
}
